/**
  ******************************************************************************
  * @file    Libraries/Device/TS32F020X/TS32F020X_LL_Driver/src/ts32f020x_ll_spi_iic.c
  * @author  JUSHENG Application Team
  * @version V1.0.0
  * @date    02-19-2022
  * @brief   This file contains all the SPI_IIC LL firmware functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 JUSHENG</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "include.h"

/** @addtogroup SPI_IIC_LL_Driver SPI_IIC LL Driver
  * @ingroup  spi_iic_interface_gr
  * @{
  */
  
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/** @defgroup SPI_IIC_LL_Init_Cfg SPI_IIC LL Initialization And Configuration
  * @ingroup  SPI_IIC_LL_Driver
  * @brief    SPI_IIC LL Initialization And Configuration
  * @{
  */

/** 
  * @brief  Link layer SPI initialization function
  * @param  p_spi: The structure pointer of the SPI is selected
  * @param  p_init : SPI initialization struct
  * @retval None
  */
void ll_spi_init(SPI_IIC_TypeDef *p_spi, TYPE_LL_SPI_INIT *p_init)
{
    /* The system resets the SPI module, Enable the clock of the module. */
    TX_ASSERT(((SPI0 == p_spi)) && (NULL != p_init));
    /* init defaule diasble module */
    p_spi->CON1 = ALL_WORD_00;
    /* clear all pending           */
    p_spi->STA  = ALL_WORD_FF;   
    p_spi->BAUD = p_init->baud;
    
    p_spi->CON0 = (p_init->cs_rising_ie ? LL_SPI_CON0_CS_RISING_EDGE_IE : 0) |
                  (p_init->lsb_en ? LL_SPI_CON0_LSB_SEL : 0) |
                  (p_init->wire_mode ? LL_SPI_CON0_WIRE_MODE_EN : 0) |
                  (p_init->cs_en ? LL_SPI_CON0_CS_EN : 0) | LL_SPI_CON0_CS_HIGH_SEL |
                  LL_SPI_CON0_MASTER_CAP_DLY_SET(p_init->delay_cycle_cnt) |
                  LL_SPI_CON0_FRAME_SIZE_SET(p_init->frame_size) |
                  LL_SPI_CON0_CPOL_CPHA_SET(p_init->spi_mode);
    
    p_spi->CON1 = (p_init->fifo_ov_ie ? LL_SSP_CON1_FIFO_OV_IE : 0) |
                  (p_init->rfifo_not_empty_ie ? LL_SSP_CON1_RFIFO_NOT_EMPTY_IE : 0) |
                  (p_init->tfifo_not_full_ie ? LL_SSP_CON1_TFIFO_NOT_FULL_IE : 0) |
                  (p_init->frame_ie ? LL_SSP_CON1_DONE_IE : 0) |
                  (p_init->work_mode ? LL_SSP_CON1_SLAVE_MODE_SEL : 0) | 
                   LL_SSP_CON1_EN;
}

/**
  * @brief  Link layer IIC initialization function
  * @param  p_iic: The structure pointer of the IIC is selected
  * @param  p_init : IIC initialization struct
  * @retval None.
  */
void ll_iic_init(SPI_IIC_TypeDef *p_iic, TYPE_LL_IIC_INIT *p_init)
{
    /* The system resets the IIC module, Enable the clock of the module. */
    TX_ASSERT(((SPI0 == p_iic)) && (NULL != p_init));
    /* init defaule diasble module */
    p_iic->CON1 = ALL_WORD_00;
    /* clear all pending           */
    p_iic->STA  = ALL_WORD_FF;
    
    p_iic->BAUD = p_init->baud;
    p_iic->CON0 = LL_IIC_CON0_FILTER_CNT_SET(0) |
                  (p_init->al_ie ? LL_IIC_CON0_AL_IE : 0) |
                  (p_init->stop_ie ? LL_IIC_CON0_STOP_IE : 0);
    p_iic->CON1 = (p_init->frame_ie ? LL_SSP_CON1_DONE_IE : 0) |
                  (p_init->fifo_ov_ie ? LL_SSP_CON1_FIFO_OV_IE : 0) |
                  (p_init->rfifo_not_empty_ie ? LL_SSP_CON1_RFIFO_NOT_EMPTY_IE : 0) |
                  (p_init->tfifo_not_full_ie ? LL_SSP_CON1_TFIFO_NOT_FULL_IE : 0) |
                  (p_init->work_mode ? LL_SSP_CON1_SLAVE_MODE_SEL : 0) |
                  LL_SSP_CON1_IIC_MODULE_SEL | LL_SSP_CON1_EN;
}

/** 
  * @brief  Link layer IIC slave config function
  * @param  p_iic: The structure pointer of the IIC is selected
  * @param  p_cfg  : IIC Configuration struct
  * @retval None
  */
void ll_iic_slave_mode_config(SPI_IIC_TypeDef *p_iic, TYPE_IIC_SLAVE_CFG *p_cfg)
{
    /* close ssp module first */
    p_iic->CON1 &= ~(LL_SSP_CON1_TX_EN | LL_SSP_CON1_EN);
    
    p_iic->CON0 |= (p_cfg->nack_ie ? LL_IIC_CON0_RX_NACK_IE : 0) |
                   (p_cfg->addr_match_ie ? LL_IIC_CON0_ADDR_MATCH_IE : 0) |
                   (p_cfg->broadcast_ie ? LL_IIC_CON0_BROADCAST_IE : 0) |
                   (p_cfg->broadcast_en ? LL_IIC_CON0_BROADCAST_EN : 0) |
                   LL_IIC_CON0_SLAVE_ADDR_SET(p_cfg->slave_addr) |
                   LL_IIC_CON0_SLAVE_ADDR_WIDTH_SET(p_cfg->addr_width);
    p_iic->CON1 |= LL_SSP_CON1_IIC_MODULE_SEL | LL_SSP_CON1_EN;
}

/**
  * @}
  */

/** @defgroup SPI_IIC_LL_Data_Transfers SPI_IIC LL Data transfers functions
  * @ingroup  SPI_IIC_LL_Driver
  * @brief    SPI_IIC LL Data transfers functions 
  * @{
  */


/**
  * @brief  Link layer SSP buf overflow data lost interrupt enable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_fifo_ov_interrupt_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 |= LL_SSP_CON1_FIFO_OV_IE;
}

/**
  * @brief  Link layer SSP buf overflow data lost interrupt disable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_fifo_ov_interrupt_disable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 &= (~LL_SSP_CON1_FIFO_OV_IE);
}

/**
  * @brief  Link layer SSP receive buf not empty interrupt enable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_rfifo_not_empty_interrupt_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 |= LL_SSP_CON1_RFIFO_NOT_EMPTY_IE;
}

/**
  * @brief  Link layer SSP receive buf not empty interrupt disable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_rfifo_not_empty_interrupt_disable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 &= (~LL_SSP_CON1_RFIFO_NOT_EMPTY_IE);
}

/**
  * @brief  Link layer SSP send buf not full interrupt enable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_tfifo_not_full_interrupt_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 |= LL_SSP_CON1_TFIFO_NOT_FULL_IE;
}

/**
  * @brief  Link layer SSP send buf not full interrupt disable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_tfifo_not_full_interrupt_disable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 &= (~LL_SSP_CON1_TFIFO_NOT_FULL_IE);
}

/**
  * @brief  Link layer SSP interrupt enable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_interrupt_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 |= LL_SSP_CON1_DONE_IE;
}

/**
  * @brief  Link layer SSP interrupt disable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_interrupt_disable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 &= (~LL_SSP_CON1_DONE_IE);
}

/**
  * @brief  Link layerSSP tx enable rx disable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_tx_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 |= LL_SSP_CON1_TX_EN;
}

/**
  * @brief  Link layer SSP tx disable rx enable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_rx_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 &= (~LL_SSP_CON1_TX_EN);
}

/**
  * @brief  Link layer SSP master mode enable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_master_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 |= LL_SSP_CON1_SLAVE_MODE_SEL;
}

/**
  * @brief  Link layer SSP slave mode enable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_slave_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 &= ~(LL_SSP_CON1_SLAVE_MODE_SEL);
}

/**
  * @brief  Link layer SSP module enable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_enable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 |= LL_SSP_CON1_EN;
}

/**
  * @brief  Link layer SSP module disable
  * @param  p_ssp: The structure pointer of the SPI or IIC is selected
  * @retval None
  */
void ll_spi_iic_disable(SPI_IIC_TypeDef *p_ssp)
{
    p_ssp->CON1 &= (~LL_SSP_CON1_EN);
}

/**
  * @brief  Link layer get IIC index(IIC0 is 0, IIC1 is 1)
  * @param  p_ssp: IICx group address(IIC0, IIC1)
  * @retval index: 0 or 1
  */
u32 ll_spi_iic_get_index(SPI_IIC_TypeDef *p_ssp)
{
    switch((u32)p_ssp) {
    case (u32)SPI0:
        return 0;
    default:
        return 1;
    }
}

/**
  * @brief  Link layer stop SPI or IIC
  * @param  p_spi: SPI or IIC group address
  * @retval none
  */
void ll_spi_iic_stop(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 = 0x0000;
    p_spi->CON1 = 0x0000;
}

/**
  * @brief  Link layer SPI clear buf count
  * @param  p_spi: SPI or IIC group address
  * @retval none
  */
void ll_spi_clear_fifo_cnt(SPI_IIC_TypeDef *p_spi)
{
    p_spi->STA |= LL_SSP_STA_FIFO_CNT_CLR;
}

/**
  * @brief  Link layer SPI or IIC fifo empty
  * @param  p_spi: SPI or IIC group address
  * @retval true or false
  */
bool ll_spi_iic_fifo_empty(SPI_IIC_TypeDef *p_spi)
{
    if(p_spi->CON1 & LL_SSP_CON1_RFIFO_NOT_EMPTY_IE) {
    /*  not empty ie enable, pending occured is means rx fifo not empty */
      if(LL_SPI_IIC_FIFO_EMPTY_PEND_GET(p_spi)) {
            return false;
        } else {
            return true;
        }
    } else {
        if(LL_SPI_IIC_FIFO_EMPTY_PEND_GET(p_spi)) {
            return true;
        } else {
            return false;
        }
    }
}

/**
  * @brief  Link layer SPI or IIC fifo full
  * @param  p_spi: SPI or IIC group address
  * @retval true or false
  */
bool ll_spi_iic_fifo_full(SPI_IIC_TypeDef *p_spi)
{
    /*  SPI IIC judge if fifo is full,it is no use for ie enable.  */
    if(LL_SPI_IIC_FIFO_FULL_PEND_GET(p_spi)) {
        return true;
    } else {
        return false;
    }
}

/**
  * @brief  Link layer SPI or IIC DMA count get
  * @param  p_spi: SPI or IIC group address
  * @retval DMA count
  */
u32 ll_spi_iic_dma_cnt_get(SPI_IIC_TypeDef *p_spi)
{
    return (p_spi->DMA_CNT);
}

/**
  * @brief  Link layer SPI cs rising edge interrupt enable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_cs_rising_edge_interrupt_enable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 |= LL_SPI_CON0_CS_RISING_EDGE_IE;
}

/**
  * @brief  Link layer SPI cs rising edge interrupt disable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_cs_rising_edge_interrupt_disable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 &= (~LL_SPI_CON0_CS_RISING_EDGE_IE);
}

/**
  * @brief  Link layer SPI transfert from the low bit enable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_low_bit_first_enable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 |= LL_SPI_CON0_LSB_SEL;
}

/**
  * @brief  Link layer SPI transfert from the low bit disable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_low_bit_first_disable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 &= (~LL_SPI_CON0_LSB_SEL);
}

/**
  * @brief  Link layer SPI cs enable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_cs_enable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 |= LL_SPI_CON0_CS_EN;
}

/**
  * @brief  Link layer SPI cs disable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_cs_disable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 &= (~LL_SPI_CON0_CS_EN);
}

/**
  * @brief  Link layer SPI slave sync enable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_slave_sync_enable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 |= LL_SPI_CON0_SLAVE_SYNC_EN;
}

/**
  * @brief  Link layer SPI slave sync disable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_slave_sync_disable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 &= (~LL_SPI_CON0_SLAVE_SYNC_EN);
}

/** 
  * @brief  Link layer SPI master sync enable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_master_sync_enable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 |= LL_SPI_CON0_MASTER_SYNC_EN;
}

/**
  * @brief  Link layer SPI master sync disable
  * @param  p_spi: The structure pointer of the SPI is selected
  * @retval None
  */
void ll_spi_master_sync_disable(SPI_IIC_TypeDef *p_spi)
{
    p_spi->CON0 &= (~LL_SPI_CON0_MASTER_SYNC_EN);
}

/**
  * @brief  Get the wire mode of the current SPI module
  * @param  p_spi: SPI module pointer
  * @retval TYPE_ENUM_LL_SPI_WIRE_MODE
  */
TYPE_ENUM_LL_SPI_WIRE_MODE ll_spi_wire_mode_get(SPI_IIC_TypeDef *p_spi)
{
    return (TYPE_ENUM_LL_SPI_WIRE_MODE)LL_SPI_CON0_WIRE_MODE_GET(p_spi);
}

/**
  * @brief  Set the SPI module's wire mode
  * @param  p_spi: SPI module pointer
  * @param  wire_mode: The wire mode to be set by the SPI module
  * @retval None
  */
void ll_spi_wire_mode_set(SPI_IIC_TypeDef *p_spi, TYPE_ENUM_LL_SPI_WIRE_MODE wire_mode)
{
    if(wire_mode)
    {
        p_spi->CON0 |= LL_SPI_CON0_WIRE_MODE_EN;
    }
    else
    {
        p_spi->CON0 &= ~(LL_SPI_CON0_WIRE_MODE_EN);
    }
}

/**
  * @brief  Pull up the chip select(CS) pins of the SPI module.
  * @param  p_spi: SPI module pointer.
  * @retval None
  * @note   Only valid in master mode.
  */
void ll_spi_cs_high(SPI_IIC_TypeDef *p_spi)
{
    (p_spi)->CON0 |= LL_SPI_CON0_CS_HIGH_SEL;
}

/**
  * @brief  Pull down the chip select(CS) pins of the SPI module.
  * @param  p_spi: SPI module pointer.
  * @retval None
  * @note   Only valid in master mode.
  */
void ll_spi_cs_low(SPI_IIC_TypeDef *p_spi)
{
    (p_spi)->CON0 &= ~LL_SPI_CON0_CS_HIGH_SEL;
}

/**
  * @brief  Link layer IIC rx nack interrupt enable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_rx_nack_interrupt_enable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 |= LL_IIC_CON0_RX_NACK_IE;
}

/**
  * @brief  Link layer IIC rx nack interrupt disable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_rx_nack_interrupt_disable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 &= (~LL_IIC_CON0_RX_NACK_IE);
}

/**
  * @brief  Link layer IIC master arbitration lost interrupt enable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_al_interrupt_enable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 |= LL_IIC_CON0_AL_IE;
}

/**
  * @brief  Link layer IIC master arbitration lost interrupt disable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_al_interrupt_disable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 &= (~LL_IIC_CON0_AL_IE);
}

/**
  * @brief  Link layer IIC master or slave detected stop signal interrupt enable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_stop_interrupt_enable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 |= LL_IIC_CON0_STOP_IE;
}

/**
  * @brief  Link layer IIC master or slave detected stop signal interrupt disable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_stop_interrupt_disable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 &= (~LL_IIC_CON0_STOP_IE);
}

/**
  * @brief  Link layer IIC slave address matched interrupt enable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_addr_match_interrupt_enable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 |= LL_IIC_CON0_ADDR_MATCH_IE;
}

/**
  * @brief  Link layer IIC slave address matched interrupt disable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_addr_match_interrupt_disable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 &= (~LL_IIC_CON0_ADDR_MATCH_IE);
}

/**
  * @brief  Link layer IIC broadcast interrupt enable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_broadcast_interrupt_enable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 |= LL_IIC_CON0_BROADCAST_IE;
}

/**
  * @brief  Link layer IIC broadcast interrupt disable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_broadcast_interrupt_disable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 &= (~LL_IIC_CON0_BROADCAST_IE);
}

/**
  * @brief  Link layer IIC broadcast enable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_broadcast_enable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 |= LL_IIC_CON0_BROADCAST_EN;
}

/**
  * @brief  Link layer IIC broadcast disable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_broadcast_disable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 &= (~LL_IIC_CON0_BROADCAST_EN);
}

/**
  * @brief  Link layer IIC response nack enable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_tx_nack_enable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 |= LL_IIC_CON0_TX_NACK_SEL;
}

/**
  * @brief  Link layer IIC response nack disable
  * @param  p_iic: The structure pointer of the IIC is selected
  * @retval None
  */
void ll_iic_tx_nack_disable(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON0 &= (~LL_IIC_CON0_TX_NACK_SEL);
}

/**
  * @brief  IIC line if busy
  * @param  p_iic: IIC module pointer
  * @retval true or false
  */
bool ll_iic_bus_is_busy(SPI_IIC_TypeDef *p_iic)
{
    return (p_iic->STA & LL_IIC_STA_BUS_BUSY_PEND) ? true : false;
}

/**
  * @brief  IIC arbitration if lost(multiple master)
  * @param  p_iic: IIC module pointer
  * @retval true or false
  */
bool ll_iic_arbitration_is_lost(SPI_IIC_TypeDef *p_iic)
{
    if(p_iic->STA & LL_IIC_STA_AL_PEND) {
        p_iic->STA = LL_IIC_STA_AL_PEND;
        return true;
    } else {
        return false;
    }
}

/**
  * @brief  IIC slave address response judge(if address matched, and write or read direction)
  * @param  p_iic: IIC module pointer
  * @retval TYPE_ENUM_LL_IIC_ADDR_RESPONSE
  */
TYPE_ENUM_LL_IIC_ADDR_RESPONSE ll_iic_slave_addr_response(SPI_IIC_TypeDef *p_iic)
{

    if(!(p_iic->STA & LL_IIC_STA_ADDR_MATCH_PEND)) {
        return LL_IIC_NO_RESPONSE;
    }
    p_iic->STA = LL_IIC_STA_ADDR_MATCH_PEND;
    if(p_iic->STA & LL_IIC_STA_SLAVE_RW) {
        return LL_IIC_READ;
    } else {
        return LL_IIC_WRITE;
    }
}

/**
  * @brief  IIC slave rx broadcase judge
  * @param  p_iic: IIC module pointer
  * @retval true or false
  */
bool ll_iic_slave_rx_broadcast(SPI_IIC_TypeDef *p_iic)
{
    if(p_iic->STA & LL_IIC_STA_BROADCAST_PEND) {
        p_iic->STA = LL_IIC_STA_BROADCAST_PEND;
        return true;
    } else {
        return false;
    }
}

/**
  * @brief  IIC get ack state
  * @param  p_iic: IIC module pointer
  * @retval TYPE_ENUM_LL_IIC_ACK
  */
TYPE_ENUM_LL_IIC_ACK ll_iic_get_ack_state(SPI_IIC_TypeDef *p_iic)
{
    return (p_iic->STA & LL_IIC_STA_RX_NACK) ? LL_IIC_NACK : LL_IIC_ACK;
}

/**
  * @brief  IIC slave rx 1 byte data
  * @param  p_iic: IIC module pointer
  * @retval 1 byte data
  */
u8 ll_iic_slave_rx_byte(SPI_IIC_TypeDef *p_iic)
{
    p_iic->CON1 &= ~LL_SSP_CON1_TX_EN;
    return p_iic->CMD_DATA;
}

/**
  * @brief  IIC slave tx 1 byte data
  * @param  p_iic: IIC module pointer
  * @param  data: 1 byte data
  * @retval none
  */
void ll_iic_slave_tx_byte(SPI_IIC_TypeDef *p_iic, u8 data)
{
    p_iic->CON1 |= LL_SSP_CON1_TX_EN;
    p_iic->CMD_DATA = data;
}

/**
  * @}
  */

/**
  * @}
  */

/*************************** (C) COPYRIGHT 2022 JUSHENG ***** END OF FILE *****/
